package com.MediaVaultTest;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.MediaVaultTest.UserStore;
import com.MediaVaultTest.Media;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import com.MediaVaultTest.PMF;
import javax.jdo.JDOObjectNotFoundException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.LinkedList;
import com.MediaVaultTest.Format;
	
public class editInfo extends HttpServlet {
	
public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws IOException {
		
		String stringKey = req.getParameter("theKey");
	
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Media media = null;
		Key mediaKey = KeyFactory.stringToKey(stringKey);
		media = pm.getObjectById(Media.class, mediaKey);
	
		String title = req.getParameter("title");
		String mediaType = req.getParameter("mediaType");
		String language = req.getParameter("language");
		String rating = req.getParameter("rating");
		Integer numRating;
		try {
			numRating = Integer.valueOf(rating);
		} catch(NumberFormatException e) {
			numRating = media.getRating();
		}
		String album = req.getParameter("album");
		String artist = req.getParameter("artist");
		String genre = req.getParameter("genre");
		String series = req.getParameter("series");
	
		
		media.setTitle(title);
		media.setMediaType(mediaType);
		media.setLanguage(language);
		media.setRating(numRating);
		media.setAlbum(album);
		media.setArtist(artist);
		media.setGenre(genre);
		media.setSeries(series);
		
		pm.makePersistent(media);
		pm.close();
		
		res.sendRedirect("/viewInfo.jsp?key="+stringKey);
	}
}

